export default {
  namespaced: true,
  state: {
    type: 'all',
    list: JSON.parse(localStorage.getItem('vuex-todos-hf')) || [],
  },
  mutations: {
    changeDone(state, id) {
      const obj = state.list.find(item => item.id === id)
      obj.isDone = !obj.isDone
    },
    delById(state, id) {
      state.list = state.list.filter(item => item.id !== id)
    },
    add(state, content) {
      state.list.unshift({
        id: +new Date(),
        name: content,
        isDone: false
      })
    },
    changeIsAll(state, val) {
      state.list.forEach(item => item.isDone = val)
    },
    clearDone(state) {
      state.list = state.list.filter(item => !item.isDone)
    },
    changeType(state, val) {
      state.type = val
    },
  },
  getters: {
    leftCount(state) {
      return state.list.filter(item => !item.isDone).length
    },
    showClear(state) {
      return state.list.some(item => item.isDone)
    },
    showList(state) {
      if (state.type === 'active') {
        // 未完成 isDone为false
        return state.list.filter((item) => !item.isDone)
      } else if (state.type === 'complete') {
        // 已完成
        return state.list.filter((item) => item.isDone)
      } else {
        // 全部数据
        return state.list
      }
    },
  }
}